import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Index from '../views/Index.vue'
import SystemSetup from '../component/HomePageSystem/SystemSetup.vue'
import AccountSetting from '../component/HomePageSystem/AccountSetting.vue'
import axios from 'axios'
import Promotion from '../views/Run/mszq/Promotion/Promotion'
import TimeList from '../views/Run/mszq/TimeList/TimeList'
import GoodList from '../component/HomePageSystem/components/GoodsShop/goodList.vue'
import AdvertisingList from '../views/Run/gggl/AdvertisingList/AdvertisingList.vue'
import Sett from '../views/Run/mszq/Promotion/set/Set.vue'
import Good from '../views/Run/mszq/Promotion/set/goods/Goods.vue'
import AddGood from '../component/HomePageSystem/components/AddGood/addGood.vue'
import GoodSet from '../views/GoodSet/GoodSet.vue'
import SelectCate from '../views/GoodSet/SelectCate/SelectCate'

axios.defaults.baseURL = 'http://localhost:8080'

axios.interceptors.request.use(config => {
  // console.log(11)
  console.log(config)
  config.headers.Authorization = window.sessionStorage.getItem('token')
  // console.log(config.headers.token)
  return config
})
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/index',
    component: Index,
    redirect: '/system-setup',
    children: [
      { path: '/', component: SystemSetup },
      { path: '/system-setup', component: SystemSetup },
      { path: '/account-setting', component: AccountSetting },
      { path: '/seckill-list', component: Promotion },
      { path: '/time-list', component: TimeList },
      { path: '/goods-list', component: GoodList },
      { path: '/ad-list', component: AdvertisingList },
      { path: '/set', component: Sett },
      { path: '/goods-lists', component: Good },
      { path: '/add-goods', component: AddGood },
      { path: '/goods-cate', component: GoodSet },
      {
        path: '/addCate',
        component: SelectCate
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 创建路由导航
router.beforeEach((to, form, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
// 路由导航接受当前点击的按键
router.afterEach((to, form, next) => {
  console.log(to.path)
  window.sessionStorage.setItem('path', to.path)
})
export default router
